#include "include.h"
#include "bsp_led.h"
#include "bsp_rgb_light.h"

#define MAX_GRB_LED 2
#define LED_ON 1
#define LED_OFF 0
#define BREATHE_ON 0
#define BREATHE_OFF 0x2800
#define MAX_LIGHT_MODE 3

led_param_t led_info;

u32 delay_ticks;





AT(.com_text.rgb)
void com_memcpy(void *dst,const void *src,size_t length)
{
	while(length--)
	{
		*(char*)dst=*(char*)src;
		dst=(char*)dst+1;
		src=(char*)src+1;
	}
}
AT(.com_text.rgb)
void com_memset(void *s,int c,size_t n)
{
	const unsigned char uc = c;
	unsigned char *su;
	for(su = s;0 < n;++su,--n)
	*su = uc;
}



AT(.text.led_disp)
void bsp_led_bt_pair_init(void)
{
	led_info.timer_start=1;
	led_info.time_out=1;
	led_info.time_cnt=1;
	LED_SET_ONOFF(LED_1,LED_OFF);
	//com_memset(rgb_led_buf,LED_OFF,MAX_GRB_LED);
	//rgb_led_set();
}
AT(.com_text.led_disp)
void bsp_led_bt_pair(void)
{
	u8 led_status;
	led_info.led_step++;
	if(led_info.led_step>1)
	{
		led_info.led_step=0;
	}
	switch(led_info.led_step)
	{
		case 1:
			led_status=LED_OFF;

			break;
		case 0:
		    led_status=LED_ON;
			break;
		default:
			break;
	}
	LED_SET_ONOFF(LED_1,led_status);
	led_info.time_cnt=0;
	led_info.time_out=10;

}

AT(.text.led_disp)
void bsp_led_bt_reconnect_init(void)
{
	led_info.timer_start=1;
	led_info.time_out=1;
	led_info.time_cnt=1;
	//com_memset(rgb_led_buf,LED_OFF,MAX_GRB_LED);
	//rgb_led_set();
}
AT(.com_text.led_disp)
void bsp_led_bt_reconnect(void)
{
	u8 led_status;
	led_info.led_step++;
	if(led_info.led_step>1)
	{
		led_info.led_step=0;
	}
	switch(led_info.led_step)
	{
		case 1:
		    led_status=LED_ON;
			break;
		case 0:
		    led_status=LED_OFF;
			break;
		default:
			break;
	}
	LED_SET_ONOFF(LED_1,led_status);
	led_info.time_cnt=0;
	led_info.time_out=30;

}
AT(.text.led_disp)
void bsp_led_bt_call_coming_init(void)
{
	led_info.timer_start=1;
	led_info.time_out=1;
	led_info.time_cnt=1;
}
AT(.com_text.led_disp)
void bsp_led_bt_call_coming(void)
{
	u8 led_status;
	led_info.led_step++;
	if(led_info.led_step>1)
	{
		led_info.led_step=0;
	}
	switch(led_info.led_step)
	{
		case 1:
		    led_status=LED_ON;
			break;
		case 0:
		    led_status=LED_OFF;
			break;
		default:
			break;
	}
	LED_SET_ONOFF(LED_1,led_status);
	led_info.time_cnt=0;
	led_info.time_out=4;

}
AT(.text.led_disp)
void bsp_led_bt_call_reject_init(void)
{
	led_info.timer_start=1;
	led_info.time_out=1;
	led_info.time_cnt=1;
}
AT(.com_text.led_disp)
void bsp_led_bt_call_reject(void)
{
	u8 led_status;
	led_info.led_step++;
	if(led_info.led_step>5)
	{
		led_info.led_step=0;
		bsp_set_led_mode(LED_BT_CONNECTED);
		return;
	}
	//printf("led_step:%d\n",led_info.led_step);
	if(led_info.led_step%2==0)
	{
		led_status=LED_ON;
	}
	else
	{
		led_status=LED_OFF;
	}
	LED_SET_ONOFF(LED_1,led_status);
	led_info.time_cnt=0;
	led_info.time_out=20;

}

AT(.text.led_disp)
void bsp_led_bt_pair_fail_init(void)
{
	led_info.timer_start=1;
	led_info.time_out=1;
	led_info.time_cnt=1;
	led_info.led_step=0;
	//com_memset(rgb_led_buf,LED_OFF,MAX_GRB_LED);
	//rgb_led_set();
}
AT(.text.led_disp)
void bsp_led_bt_connected_init(void)
{
	led_info.timer_start=0;
	LED_SET_ONOFF(LED_1,LED_ON);
}
AT(.text.led_disp)
void bsp_led_poweron_init(void)
{
	led_info.timer_start=0;
	LED_SET_ONOFF(LED_1,LED_ON);
}
AT(.text.led_disp)
void bsp_led_poweroff_init(void)
{
	led_info.timer_start=0;
	LED_SET_ONOFF(LED_1,LED_OFF);
}

AT(.com_text.led_disp)
void bsp_led_bt_pair_fail(void)
{
	u8 led_status;
	led_info.led_step++;
	if(led_info.led_step>5)
	{
		led_info.led_step=0;
		bsp_set_led_mode(LED_BT_IDLE);
		return;
	}
	//printf("led_step:%d\n",led_info.led_step);
	if(led_info.led_step%2==0)
	{
		led_status=LED_ON;
	}
	else
	{
		led_status=LED_OFF;
	}
	LED_SET_ONOFF(LED_1,led_status);
	led_info.time_cnt=0;
	led_info.time_out=5;

}

AT(.com_text.led_disp)
void bsp_led_bt_timer(void)
{
	//if(tick_check_expire(delay_ticks,50))
	{
		//delay_ticks = tick_get();
		if(!led_info.timer_start)
		{
			return;
		}
		led_info.time_cnt++;
		if(led_info.time_cnt<led_info.time_out)
		{
			return;
		}
		switch(led_info.led_mode)
		{
			case LED_BT_PAIR:
				bsp_led_bt_pair();
				break;
			case LED_BT_RECONNECT:
			case LED_BT_IDLE:
				bsp_led_bt_reconnect();
				break;
			case LED_BT_PAIR_FAIL:
				bsp_led_bt_pair_fail();
				break;
			case LED_BT_CALL_COMING:
				bsp_led_bt_call_coming();
				break;
			case LED_BT_CALL_REJECT:
				bsp_led_bt_call_reject();
				break;
			default:
				break;
		}

	}
}



AT(.com_text.led_disp)
void bsp_led_timer(void)
{
	if(led_info.led_mode!=0)
	{
		//bsp_led_Sequence_display();
		bsp_led_bt_timer();
	}
}
AT(.com_text.led_disp)
void bsp_sequence_display_timer(void)
{
	//bsp_led_bt_timer();
	//bsp_light_grow_timer();
}

void bsp_led_mode_init(void)
{
	printf("led mode:%d\n",led_info.led_mode);
	switch(led_info.led_mode)
	{
		case LED_BT_PAIR:
			bsp_led_bt_pair_init();
			break;
		case LED_BT_IDLE:
		case LED_BT_RECONNECT:
			bsp_led_bt_reconnect_init();
			break;
		case LED_BT_PAIR_FAIL:
			bsp_led_bt_pair_fail_init();
			break;
		case LED_POWER_ON:
		case LED_BT_CONNECTED:
			bsp_led_bt_connected_init();
			break;
		case LED_POWER_OFF:
			bsp_led_poweroff_init();
			break;
		case LED_BT_CALL_COMING:
			bsp_led_bt_call_coming_init();
			break;
		case LED_BT_CALL_REJECT:
			bsp_led_bt_call_reject_init();
			break;
		default:
		break;
	}
}
#if 0

u8 test_mode_index=0;

void test_color(void)
{
	printf("test_mode_index:%d\n",test_mode_index);
	//memset(rgb_led_buf,LED_OFF,MAX_GRB_LED);
	//rgb_led_buf[test_mode_index]=LED_ON;
	//LED_SET_ONOFF(test_mode_index,LED_ON);
	com_memset(rgb_led_buf,LED_OFF,MAX_GRB_LED);
	if(test_mode_index>7)
	{
		test_mode_index=0;
	}
	else
	{
		rgb_led_buf[test_mode_index]=LED_ON;
	}
	rgb_led_set();
	test_mode_index++;
	#if 0
	if(test_mode_index>=LED_SEQUENCE_10B)
	{
		test_mode_index=LED_SEQUENCE_1;
	}
	bsp_set_led_mode(test_mode_index);
	#endif
	//rgb_led_set();
}
#endif

void bsp_set_led_mode(u8 led_mode)
{
	if(led_info.led_mode==led_mode)
	{
		return;
	}
	led_info.led_mode=led_mode;
	bsp_led_mode_init();
}

